package com.lanxiao.cloud.modules.user.application;

import com.lanxiao.cloud.common.core.service.IOperatorService;
import com.lanxiao.cloud.modules.user.application.assembler.UserAssembler;
import com.lanxiao.cloud.modules.user.application.dto.UserCmdUpdate;
import com.lanxiao.cloud.modules.user.application.dto.UserPersonalCmdUpdate;
import com.lanxiao.cloud.modules.user.domain.model.User;
import com.lanxiao.cloud.modules.user.domain.service.IUserService;
import com.lanxiao.cloud.user.api.dto.LoginInfoDubboUpdate;
import com.lanxiao.cloud.user.api.dto.UserDubboAdd;
import org.springframework.stereotype.Component;

/**
 * @author ：qiaoliang
 * @date ：2021-09-13
 */
@Component
public class UserCmdManager {

  private final IUserService userService;
  private final UserAssembler userAssembler;
  private final IOperatorService operatorService;

  public UserCmdManager(IUserService userService,
      UserAssembler userAssembler,
      IOperatorService operatorService) {
    this.userService = userService;
    this.userAssembler = userAssembler;
    this.operatorService = operatorService;
  }

  /**
   *  注册
   *
   * @author ：qiaoliang
   * @param userDubboAdd : 用户信息
   * @date 2021-09-14 15:09
   */
  public Long register(UserDubboAdd userDubboAdd) {
    final User user = userAssembler.toUser(userDubboAdd);
    userService.save(user);
    return user.getId();
  }

  /**
   *  修改用户登录信息
   *
   * @author ：qiaoliang
   * @param loginInfoDubboUpdate :  登录信息
   * @date 2021-09-13 11:19
   */
  public void loginInfoUpdate(LoginInfoDubboUpdate loginInfoDubboUpdate) {
    userService.updateUser(userAssembler.toUser(loginInfoDubboUpdate));
  }

  /**
   *  修改用户信息
   *
   * @author ：qiaoliang
   * @param id : 主键
   * @param update : 修改信息
   * @date 2021-09-14 13:51
   */
  public void update(Long id, UserCmdUpdate update) {
    userService.updateUser(userAssembler.toUser(id, update));
  }
  public void update(UserPersonalCmdUpdate update) {
    userService.updateUser(userAssembler.toUser(operatorService.operator(), update));
  }
  public void updateDept(Long id, Long deptId) {
    userService.updateUser(new User(id, deptId));
  }

}
